/*-
 * Copyright (c) 2006, 2007 FTS Team
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 4. Neither the name of the FTS Team nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *      RetrieverWorker.cpp
 *
 * $FTS$
 */

// C Includes
#include <fcntl.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>

// Local Includes
#include <FSDumper.hpp>
#include <MemoryInvIndexStorage.hpp>

using namespace FTSS; // FullTextSearchSystem

int main(int argc, char ** argv)
{
	if (argc != 2) { fprintf(stdout, "usage: %s index_file.dat\n", argv[0]); return EX_USAGE; }

	MemoryInvIndexStorage oMemoryStorage;
	InvIndexStorage & oStorage = oMemoryStorage;

	int iFileHandle = open(argv[1], O_RDONLY, 0644);
	if (iFileHandle != -1)
	{
		FSDumper oDumper(iFileHandle);
		if (oMemoryStorage.Restore(&oDumper) == -1)
		{
			fprintf(stdout, "Cannot restore index.\n");
			return EX_SOFTWARE;
		}
		close(iFileHandle);
	}
	else { fprintf(stdout, "ERROR: Cannot open index file.\n"); return EX_SOFTWARE; }

	setvbuf(stderr, NULL, _IONBF, 0);
	try
	{
		CHAR_8 szBuf[1024 + 1];
		for(;;)
		{
			fprintf(stdout, "> ");
			if (fgets(szBuf, 1024, stdin) != NULL)
			{
				szBuf[strlen(szBuf) - 1] = '\0';
				WordId_t iWordId = HashFunc(szBuf, strlen(szBuf));
				fprintf(stdout, "word: `%s' (%lu)\n", szBuf, long(iWordId));

				Pair<DocumentEntryIterator, DocumentEntryIterator> oDocEntryPair = oStorage.GetDocumentEntry(iWordId);
				if (oDocEntryPair.first != oDocEntryPair.second)
				{
fprintf(stdout, "size = %d, iDocEntrySize = %d\n", (*oDocEntryPair.first).size, oDocEntryPair.second - oDocEntryPair.first);
//					Offset_t iOffset = 0;
					W_FLOAT dLogN_DF = log(1.0 * oStorage.GetDocNum() / oStorage.GetDF(iWordId));
fprintf(stdout, "N = %d, DF = %d, dLogN_DF = %f\n", oStorage.GetDocNum(), oStorage.GetDF(iWordId), dLogN_DF);

					DocumentEntryIterator oIt = oDocEntryPair.first;
					while(oIt != oDocEntryPair.second)
					{
						// TF.IDF = log(1 + tf) * log(N / df)
						W_FLOAT dTFIDF = log(W_FLOAT((*oIt).tf() + 1)) * dLogN_DF;
						fprintf(stdout, "DocId %lu, size %d, TFIDF: %f, positions: ", long((*oIt).doc_id), (*oIt).size, dTFIDF);

						Offset_t iWEOffset = sizeof(DocumentEntry) - sizeof(WordEntry);
						WordEntry * aWE = (*oIt).words;
						while (iWEOffset < (*oIt).size)
						{
							fprintf(stdout, "%d ", aWE -> GetPosition());
							iWEOffset += sizeof(WordEntry);
							++aWE;
						}
						fprintf(stdout, "\n\n");
/*
						// TF.IDF = log(1 + tf) * log(N / df)
						W_FLOAT dTFIDF = log(pDocEnty -> tf() + 1) * dLogN_DF;
						fprintf(stdout, "DocId %llu, size %d, TFIDF: %f, positions: ", pDocEnty -> doc_id, pDocEnty -> size, dTFIDF);
						Offset_t iWEOffset = sizeof(DocumentEntry) - sizeof(WordEntry);
						WordEntry * aWE = pDocEnty -> words;

						while (iWEOffset < pDocEnty -> size)
						{
							fprintf(stdout, "%d ", aWE -> GetPosition());
							iWEOffset += sizeof(WordEntry);
							++aWE;
						}
						fprintf(stdout, "\n\n");

						iOffset += pDocEnty -> size;
						pDocEnty = (DocumentEntry * )((CHAR_8 *)pDocEnty + pDocEnty -> size);
*/
						++oIt;
					}
					fprintf(stdout, "\n\n");
				}
			}
		}
	}
	catch(...)
	{
		;;
	}

	// make valgrind happy
	fclose(stdin);
	fclose(stdout);
	fclose(stderr);

return EX_OK;
}
// End.
